package com.nuonuo.util.error;

import java.util.ArrayList;
import java.util.List;

/**
 * @author duan
 * @version 1.0
 * @date 2024/2/21 10:53
 */
public class ExceptionInInitializerErrorTest {

    private static ExceptionInInitializerErrorTest sp = buildStaticParams();

    // 1
    private static int NUM_A = getA();
    private static int NUM_B = getB();

    // 2
//    private static int NUM_B = getB();
//    private static int NUM_A = getA();


    private static List<String> LIST_A = getListA();

    private ExceptionInInitializerErrorTest() {
        System.out.println("初始化构造方法");
    }

    public static ExceptionInInitializerErrorTest getInstance() {
        return new ExceptionInInitializerErrorTest();
    }


    /**
     * sp的声明在其他几个静态变量之前
     *
     * @return
     */
    private static ExceptionInInitializerErrorTest buildStaticParams() {
        if (null == sp) {
            sp = new ExceptionInInitializerErrorTest();
        }
        int result = NUM_A * NUM_B; // 基本类型有默认值，此处不会报错，但结果不正确
        System.out.println("result is :" + result);
        LIST_A.add("abcd");		//此时LIST_A还未初始化，到此有异常
        return sp;
    }

    private static int getA() {
        System.out.println("初始化A");
        return 10;
    }

    private static int getB() {
        System.out.println("初始化B");
        return 20;
    }

    private static List<String> getListA() {
        System.out.println("初始化List");
        return new ArrayList<String>();
    }

    public static void main(String[] args) {
        ExceptionInInitializerErrorTest.getInstance();
    }
}
